package com.geovis.emergency.spd.controller.command;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.geovis.common.core.api.Result;
import com.geovis.common.core.controller.BaseController;
import com.geovis.emergency.spd.biz.pc.service.IPcDrillResponseInfoService;
import com.geovis.emergency.spd.cache.DictCache;
import com.geovis.emergency.spd.entity.command.pojo.vo.CommandDrillPublishInfoVO;
import com.geovis.emergency.spd.entity.command.pojo.vo.CommandDrillResponseInfoVO;
import com.geovis.emergency.spd.entity.pc.entity.PcDrillPublishInfo;
import com.geovis.emergency.spd.entity.pc.entity.PcDrillResponseInfo;
import com.geovis.emergency.spd.entity.pc.pojo.constant.PcDisasterConstant;
import com.geovis.emergency.spd.entity.system.entity.SystemDictData;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *
 * </p>
 *
 * @author Lonsho
 * @version 1.0
 * @date 2022/11/3
 */
@RestController
@RequestMapping("/command/drill/response/info")
@RequiredArgsConstructor
@Api(value = "指挥调度大屏-应急响应列表接口", tags = "指挥调度大屏-应急响应列表接口")
@Slf4j
@Validated
@ApiSort(4)
public class CommandDrillResponseInfoController extends BaseController<IPcDrillResponseInfoService> {
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="header",name="appId",dataType="String",required=true,value="授权id"),
            @ApiImplicitParam(paramType="header",name="timestamp",dataType="String",required=true,value="当前时间戳(秒)"),
            @ApiImplicitParam(paramType="header",name="sign",dataType="String",required=true,value="签名"),
            @ApiImplicitParam(paramType="path",name="mainEventId",dataType="String",required=true,value="演练主事件id")
    })
    @ApiOperation(value = "应急响应列表", notes = "应急响应列表，签名生成方法：md5(appId+appSecret+timestamp)")
    @GetMapping("/list/{mainEventId}")
    public Result<List<CommandDrillResponseInfoVO>> list(@PathVariable("mainEventId") String mainEventId) {
        List<PcDrillResponseInfo> list = baseService.list(
                Wrappers.lambdaQuery(PcDrillResponseInfo.class)
                        .eq(PcDrillResponseInfo::getEventId, mainEventId)
                        .orderByDesc(PcDrillResponseInfo::getCreateTime)
        );
        List<CommandDrillResponseInfoVO> collect = list.stream().map(s -> {
            CommandDrillResponseInfoVO commandDrillResponseInfoVO = BeanUtil.toBean(s, CommandDrillResponseInfoVO.class);
            if (ObjectUtil.isNotNull(commandDrillResponseInfoVO.getLevel())) {
                commandDrillResponseInfoVO.setLevelName(DictCache.getDictValue(PcDisasterConstant.EVENT_LEVEL, commandDrillResponseInfoVO.getLevel()));
            }
            return commandDrillResponseInfoVO;
        }).collect(Collectors.toList());
        return Result.ok(collect);
    }
}
